﻿using Ext.Net;

using System;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using NOAH.CMS.Repository;


public partial class _Default : System.Web.UI.Page
{
    public int currentMonth = 0;
    public int currentYear = 0;

    public int previousMonth = 0;
    public int previousYear = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (X.IsAjaxRequest) return;


        //Staff staff = new UserProfileHelper().GetFullInfoOfCurrentUser();
        //if (staff != null)
        //{
        //    string name = staff.Name;
        //    //if (staff.Department != null)
        //    //    if (staff.Department.Name != "")
        //    //    {
        //    //        name += "- " + staff.Department.Name;
        //    //    }
        //    btnProfile.Text = name;
        //}
        //else
        //{
        //    btnProfile.Text = HttpContext.Current.User.Identity.Name;
        //}

        currentMonth = DateTime.Now.Month;
        currentYear = DateTime.Now.Year;

        if (currentMonth > 1)
        {
            previousMonth = currentMonth - 1;
            previousYear = currentYear;
        }
        else
        {
            previousMonth = 12;
            previousYear = currentYear - 1;
        }

        CheckPermission();
        ShowStats();
    }

    [DirectMethod]
    public void Signout(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect(FormsAuthentication.LoginUrl);
    }

    [DirectMethod]
    public void CreateWindow(string windowID, string url, string title, int width, int heigth, bool isModal)
    {
        this.CreateWindow(base.Form, windowID, url, title, width, heigth, isModal);
    }

    public void CreateWindow(HtmlForm form, string windowID, string url, string title, int width, int height, bool isModal)
    {
        Window newWindow = new Window
        {
            ID = windowID,
            Title = title,
            Height = height,
            Width = width,
            Padding = 0,
            Modal = isModal,
            BodyBorder = false
        };
        newWindow.AutoLoad.Url = url;
        newWindow.AutoLoad.ShowMask = true;
        newWindow.AutoLoad.Mode = LoadMode.IFrame;
        newWindow.AutoLoad.MaskMsg = "Đang nạp dữ liệu, vui l\x00f2ng chờ....";
        Ext.Net.Button btnClose = new Ext.Net.Button
        {
            Icon = Ext.Net.Icon.ArrowUndo,
            Text = "Hủy bỏ"
        };
        btnClose.Listeners.Click.Handler = windowID + ".hide();";
        newWindow.Buttons.Add(btnClose);
        newWindow.Render(form);
    }


    [DirectMethod]
    public void CreateWindowDefault(string windowID, string url, string title, bool isModal)
    {
        this.CreateWindow(base.Form, windowID, url, title, 680, 480, isModal);
    }



    [DirectMethod]
    public void ShowMessage(string message)
    {
        Util.ShowMessage(message);
    }

    [DirectMethod]
    public static void ShowMessage(string title, string message)
    {
        Util.ShowMessage(title, message);
    }

    [DirectMethod]
    public static void ShowMessage2(string message)
    {
        Util.ShowMessage(message);
    }

    private void ShowStats()
    {
        int? totalNews = 0;
        int? totalNewsCategory = 0;
        int? totalLink = 0;
        int? totalFilm = 0;
        int? totalQuestion = 0;
        int? totalNotAnswerQuestion = 0;

        int? totalDocument = 0;
        int? totalDocumentType = 0;
        int? totalDocumentPublisher = 0;
        int? totalDocumentSubject = 0;
        int? totalQuestionCategory = 0;

        using (NOAH.CMS.Repository.CMSDataContext dataContext = new NOAH.CMS.Repository.CMSDataContext())
        {
            //dataContext.Stat(ref totalNews, ref totalNewsCategory, ref totalLink, ref totalFilm, ref totalNotAnswerQuestion, ref totalQuestion, ref totalQuestionCategory,
            //    ref totalDocument, ref totalDocumentType, ref totalDocumentPublisher, ref totalDocumentSubject);
        }

        lblTotalNewsArtilcle.Text = totalNews.ToString();
        lblTotalLink.Text = totalLink.ToString();
        lblTotalFilm.Text = totalFilm.ToString();
        lblTotalNewsCategory.Text = totalNewsCategory.ToString();

        //lblNotAnwserQuestion.Text = totalNotAnswerQuestion.ToString();
        //lblQuestionSubject.Text = totalQuestionCategory.ToString();
        //lblAnwserQuestion.Text = (totalQuestion - totalNotAnswerQuestion).ToString();
        //lblTotalQuestion.Text = totalQuestion.ToString();

        //lblTotalDocument.Text = totalDocument.ToString();
        //lblTotalDocumentPublisher.Text = totalDocumentPublisher.ToString();
        //lblTotalDocumentSubject.Text = totalDocumentSubject.ToString();
        //lblTotalDocumentType.Text = totalDocumentType.ToString();



    }


    private void CheckPermission()
    {
        string userName = HttpContext.Current.User.Identity.Name;

     
        mnuCommonInfo.Visible = false;
      
        mnuSystem.Visible = false;
   
        mnuWebContent.Visible = false;
        btnAddNewArticle.Visible = false;


        if (userName.ToLower() == "administrator" || userName.ToLower() == "admin")
        {
            SetControlByPermission(PermissionCode.Administrators);
            if (PermissionHelper.HasPermission(userName, PermissionCode.TeamChef))
            {
                SetControlByPermission(PermissionCode.TeamChef);
            }
        }
        else
        {
            if (PermissionHelper.HasPermission(userName, PermissionCode.Administrators))
            {
                SetControlByPermission(PermissionCode.Administrators);
            }
            if (PermissionHelper.HasPermission(userName, PermissionCode.Manager))
            {
                SetControlByPermission(PermissionCode.Manager);
            }
            if (PermissionHelper.HasPermission(userName, PermissionCode.TeamChef))
            {
                SetControlByPermission(PermissionCode.TeamChef);
            }
            if (PermissionHelper.HasPermission(userName, PermissionCode.WebContent))
            {
                SetControlByPermission(PermissionCode.WebContent);
            }
        }
    }


    private void SetControlByPermission(string code)
    {
        if (code == PermissionCode.Administrators)
        {
            mnuCommonInfo.Visible = true;
            mnuSystem.Visible = true;
            //mnuTeamReport.Visible = true;
            mnuWebContent.Visible = true;
            btnAddNewArticle.Visible = true;

        }

        if (code == PermissionCode.WebContent)
        {
            mnuWebContent.Visible = true;
            btnAddNewArticle.Visible = true;

        }

        if (code == PermissionCode.TeamChef)
        {
        }

        if (code == PermissionCode.Manager)
        {
            mnuCommonInfo.Visible = true;
        }
    }


}